package org.codeanalyzer.report;

import org.codeanalyzer.model.ApiClass;
import org.codeanalyzer.model.ApiLibrary;
import org.codeanalyzer.model.ApiMethod;
import org.codeanalyzer.model.ApiPackage;

/**
 * Creates simple text report and prints it to System.out
 */
class TxtUsageReportBuilder extends UsageReportBuilder
{
    @Override
    public void printReport(ApiLibrary apiLibrary, long elapsedTime, int numOfFiles) {
        System.out.println("                       === Analyzis results ===");
        System.out.println("Analyzed " + numOfFiles + " files. Elapsed time: " + elapsedTime + "ms");
        System.out
            .println(" -------------------------------------------------------------------------------------------");
        System.out
            .println("|         Covered packages                               | Method Coverage | Class coverage |");
        System.out
            .println(" -------------------------------------------------------------------------------------------");
        System.out.println(String.format("| %-42s %2d%% %3d/%-3d | %2d%% %5d/%-5d | %2d%% %4d/%-4d  |",
            apiLibrary.getName(),
            apiLibrary.getPackageCoverage(), apiLibrary.getTotalCoveredPackages(), apiLibrary.getTotalPackages(),
            apiLibrary.getMethodCoverage(), apiLibrary.getTotalCoveredMethods(), apiLibrary.getTotalMethods(),
            apiLibrary.getClassCoverage(), apiLibrary.getTotalCoveredClasses(), apiLibrary.getTotalClasses()));
        for(ApiPackage apiPackage : apiLibrary.getPackages())
        {
            if ( !apiPackage.isCovered())
                continue;

            System.out.println(" --------------------------------------------------------");
            System.out.println(String.format("|%-55s | %2d%% %5d/%-5d | %2d%% %4d/%-4d  |",
                apiPackage.getName(),
                apiPackage.getMethodCoverage(), apiPackage.getTotalCoveredMethods(), apiPackage.getTotalMethods(),
                apiPackage.getClassCoverage(), apiPackage.getTotalCoveredClasses(), apiPackage.getTotalClasses()));

            for(ApiClass apiClass : apiPackage.getClasses())
            {
                if ( !apiClass.isCovered())
                    continue;
                System.out
                    .println(" -------------------------------------------------------------------------------------------");
                System.out.println(String.format("|  %-53s | %2d%% %5d/%-5d |                |",
                    apiClass.getSimpleName(),
                    apiClass.getMethodCoverage(), apiClass.getTotalCoveredMethods(), apiClass.getTotalMethods()));

                for(ApiMethod apiMethod : apiClass.getMethods())
                {
                    if ( !apiMethod.isCovered())
                        continue;
                    System.out.println(String.format("|    %-51s |                 |                |",
                        apiMethod.getSimpleName() + "(" + apiMethod.getParameters() + ")"));
                }
            }
            System.out
                .println(" -------------------------------------------------------------------------------------------");
            System.out.println("\n");
        }
    }
}
